package com.thti.mecool.client.model.biz;

import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import com.thti.mecool.client.db.DataBase;
import com.thti.mecool.client.model.po.Synchro;

public class SynchroBizImpl {
	private static DataBase database;

	public SynchroBizImpl(Context context) {
		database = new DataBase(context);
	}
	
	/**
	 * 查找全部要上传的服务
	 * @return 
	 * */
	@SuppressWarnings("unchecked")
	public List<Synchro> getselectAll() {
		String sql = "select * from synchro";
		List<Synchro> list =database.findAll(Synchro.class, sql, new String[]{});
		return list;
	}
	/**
	 * 增加(网络不通保存本地数据库，等待下次上传)
	 * */
	public void save(Synchro synchro) {
		String sql = "INSERT into synchro(tablename,cid,surfaceID,url,commend)VALUES(?,?,?,?,?)";
		database.execObject(
				sql,
				new Object[] { synchro.getTableName(), synchro.getCid(),
						synchro.getSurfaceID(), synchro.getUrl(),
						synchro.getCommend() });
		database.close();
	}
	
	/**
	 * 增加(网络不通保存本地数据库，等待下次上传)
	 * */
	public static void save_zhan(String synchro) {
		String sql = "INSERT into synchro(url)VALUES(?)";
		database.execObject(sql,new Object[] { synchro});
		database.close();
	}
	/**
	 * 修改(网络不通保存本地数据库，等待下次上传)
	 * */
	public void update(Synchro synchro) {
		String sql = "update synchro set url=?, commend=? where cid=?";
		database.execObject(
				sql,
				new Object[] { synchro.getUrl(), synchro.getCommend(),
						synchro.getCid() });
		database.close();
	}
	
	/**
	 * 删除(网络不通保存本地数据库，等待下次上传)
	 * */
	public void date(int jid) {
		String sql = "delete from synchro where cid=?";
		database.execObject(
				sql,
				new Object[] {jid});
		database.close();
	}
	
	/**
	 * 根据id删除(网络不通保存本地数据库，等待下次上传)
	 * */
	public void id_delete(int id) {
		String sql = "delete from synchro where id=?";
		database.execObject(sql,
				new Object[]{id});
		database.close();
	}
	// 判断是否重复操作，是侧修改数据
	@SuppressWarnings({ "unchecked" })
	public boolean judge_exist(Synchro synchro) {
		String sql = "select * from synchro where tablename='"
				+ synchro.getTableName() + "' and cid=" + synchro.getCid()
				+ " and surfaceID=" + synchro.getSurfaceID();
		List<Synchro> list = database.findAll(Synchro.class, sql, null);
		if (list.size() > 0) {
			return true;
		}
		return false;
	}
	
	//保存sdcard路径到数据库	
    public void insertImagePath(String mapPath){
    	ContentValues values=new ContentValues();
    	values.put("path", mapPath);
    	database.insert("photo",values);
    }
	
	
}
